package org.sys.services;

import java.util.Date;

import org.base.vo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.sys.dao.IAuthenticationDao;
import org.sys.vo.Authentication;

@Service
public class LoginService {

	@Autowired
	private IAuthenticationDao iAuthentication;
	
	public Message login(Authentication authentication) {
		Authentication one = iAuthentication.selectOne(authentication);

		Message message = new Message();
		// 用户不存在
		if (one == null) {
			message.setExecuteState("01");
			message.setExecuteResult("该用户不存在！");
			return message;
		}

		// 密码错误
		if (!one.getPassword().equals(authentication.getPassword())) {
			message.setExecuteState("02");
			message.setExecuteResult("您输入的密码有误，请重新输入！");
			return message;
		}

		if (one.getPasswdExpDays() != 0) {
			Date expDate = one.getExpDate();
			// 密码已过期
			if (expDate.getTime() >= System.currentTimeMillis()) {
				message.setExecuteState("03");
				message.setExecuteResult("您的密码已过期，请修改密码后重新登录！");
				return message;
			}

			// 密码即将过期（7天）
			long betweenTimes = (System.currentTimeMillis()-expDate.getTime())/(1000*3600*24);
			int betweenDays = Integer.parseInt(String.valueOf(betweenTimes));
			if (betweenDays <= 7){
				message.setExecuteState("04");
				message.setExecuteResult("您的密码"+betweenDays+"后即将过期，请及时修改密码！");
				return message;
			}
		}

		// 登录成功
		message.setExecuteState("00");
		message.setExecuteResult("登录成功");
		return message;
	}
}
